package com.anrongtec.zcpt.tab01;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.adapter.MainTabAdapter;
import com.anrongtec.zcpt.base.BaseFragment;
import com.anrongtec.zcpt.manager.AppStateManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页fragment
 * A simple {@link Fragment} subclass.
 */
public class TabMainFragment extends BaseFragment {


    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private TabFirstFragment tab1;//原来的
//    private TabFirstFragmentOld tab1;//将下载的包保存在数据库,首页显示的是这个
    private ManagerAppsFragment tab2;
//    private AppsManagerFragment tab2;
    private List<Fragment> tabs;
    private MainTabAdapter mAdapter;
    private AppStateManager manager;
//    private AppsUnloadActivity appsUnloadActivity;

    public TabMainFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab_main, container, false);
        initData();
        initViews(view);
        registerAppStatus();
        return view;
    }

    /**
     * TODO 注册广播接收器
     */
    private void registerAppStatus() {
        manager = new AppStateManager(getActivity());
        manager.begin(new AppStateManager.AppStateListener() {

            @Override
            public void onAppUninstalled(String pkgName) {
                tab1.refresh();
                if (tab2 != null) {
                    tab2.refresh();
                }

            }

            @Override
            public void onAppRelpaced(String pkgName) {
                tab1.refresh();
                if (tab2 != null) {
                    tab2.refresh();
                }
            }

            @Override
            public void onAppInstalled(String pkgName) {
                tab1.refresh();
                if (tab2 != null) {
                    tab2.refresh();
                }
            }
        }, true);
    }

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout_main);
        mViewPager = (ViewPager) view.findViewById(R.id.view_pager_main_fragment);
        mViewPager.setAdapter(mAdapter);

        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
    }

    @Override
    protected void initData() {
        super.initData();
        tab1 = new TabFirstFragment();//2018/1/18 改
//        tab1 = new TabFirstFragmentOld();
        tab2 = new ManagerAppsFragment();
//        tab2 = new ManagerAppsFragment();
        tabs = new ArrayList<>();
        tabs.add(tab1);
        tabs.add(tab2);
        mAdapter = new MainTabAdapter(getChildFragmentManager(), tabs);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        manager.unregisterListener();
    }
}
